// Animation script by.: Phento
#include <a_samp>
//#include <sscanf2>

#define FILENAME	"p_animation.pwn"
#define MAXANIM		(1812)
#define NL			"\r\n"
#define TAB			"\t"
#define NLT			"\r\n\t"

public OnFilterScriptInit()
{
	AnimFile();
	return 1;
}

stock
	AnimFile()
{
	if(fexist(FILENAME))  { fremove(FILENAME); printf("File \'%s\' removed.", FILENAME); }
	new
		buffer[512],
		animlib[32],
		animname[32],
		checkanimlib[32],
		caseindex = 1,
		oneanim,
		oneanimtmp[512],
		oneanimtmp2[512],
		File:jjj = fopen(FILENAME, io_append);
	if(jjj)
	{
		printf("Writting %s", FILENAME);
		fwrite(jjj, "//Animation Script Generated. 2010.09.18 by.: Phento"#NL""#NL"");
		format(buffer, 512, "\
		#include <a_samp>"#NL""#NL"\
		#define FILTERSCRIPT"#NL"\
		#include <zcmd>"#NL"\
		#include <sscanf2>"#NL""#NL"\
		#define COLOR_ANIM"#TAB""#TAB"0xFF6347AA"#NL"\
		#define SPECIAL_ACTION_PISSING  68"#NL"\
		#define pressed(%%0) \\"#NLT"\
			(((newkeys & (%%0)) == (%%0)) && ((oldkeys & (%%0)) != (%%0)))"#NL""#NL"\
		new"#NLT"\
			bool:PlayerUsingAnim[MAX_PLAYERS],"#NLT"\
		Text:txtAnim;"#NL"");
	
		fwrite(jjj, buffer);

		AnimListcmds_Function(jjj);		
		ApplyAnimation_Function(jjj);
		OnPlayerKeys_Function(jjj);
		StopAnimation_Function(jjj);
		OnFilterScriptInit_Function(jjj);
		OnFilterScriptExit_Function(jjj);
		OnPlayerDeath_Function(jjj);
		
		for(new i = 1; i < MAXANIM; i++)
		{
			GetAnimationName(i, animlib, 32, animname, 32);

			if(strcmp(animlib, checkanimlib) == 0)
			{	
				if(caseindex == 1)
				{
					switch(++oneanim)
					{
						case 1:
						{
							format(oneanimtmp, 512, "COMMAND:anim%s(playerid, params[])"#NL"{"#NLT"ApplyAnimation2(params, playerid, \"%s\", \"%s\");"#NLT"return 1;"#NL"}"#NL""#NL"", tolowerstring(animlib), animlib, animname);
							format(oneanimtmp2, 512, ""#TAB""#TAB"case %d: ApplyAnimation2(params, playerid, \"%s\", \"%s\");"#NL"", caseindex, animlib, animname);
						}
						case 2:
						{
							fwrite(jjj, oneanimtmp);
						
							CommandLine(jjj, animlib);
						
							format(buffer, 512, ""#TAB""#TAB"case %d: ApplyAnimation2(params, playerid, \"%s\", \"%s\");"#NL"", caseindex, animlib, animname);
							fwrite(jjj, buffer);
							oneanim = 0;
						}
					}
				}
				
				if(caseindex > 1)
				{
					if(oneanim == 1)
					{
						CommandLine(jjj, animlib);
						fwrite(jjj, oneanimtmp2);
						oneanim = 0;
					}
					format(buffer, 512, ""#TAB""#TAB"case %d: ApplyAnimation2(params, playerid, \"%s\", \"%s\");"#NL"", caseindex, animlib, animname);
					fwrite(jjj, buffer);
				}
				
				if(i == MAXANIM - 1)
				{
					format(buffer, 512, ""#TAB""#TAB"default: SendClientMessage(playerid, COLOR_ANIM, \" Usage: /anim%s [1-%d]\");"#NLT"}"#NLT"return 1;"#NL"}"#NL"", tolowerstring(animlib), caseindex);
					fwrite(jjj, buffer);
				}
				caseindex++;
			} else {
				if(caseindex > 2)
				{
					format(buffer, 512, ""#TAB""#TAB"default: SendClientMessage(playerid, COLOR_ANIM, \" Usage: /anim%s [1-%d]\");"#NLT"}"#NLT"return 1;"#NL"}"#NL""#NL"", tolowerstring(checkanimlib), caseindex-1);
					fwrite(jjj, buffer);
				} else {
					fwrite(jjj, ""#NL"");
				}
				caseindex = 1;
				i--;
			}
			format(checkanimlib, 32, "%s", animlib);
		}
	}
	Specialaction_Function(jjj);
	fclose(jjj);
	printf("Generated \'%s\' succesfull.", FILENAME);
	return 1;
}

stock
	CommandLine(File:jj, str[])
{
	new
		string[512];
	format(string, 512, "COMMAND:anim%s(playerid, params[])"#NL"{"#NLT"new"#NLT""#TAB"animid;"#NLT"sscanf(params, \"d\", animid);"#NLT"switch(animid)"#NLT"{"#NL"", tolowerstring(str));
	fwrite(jj, string);
}

stock
	AnimListcmds_Function(File:jj)
{
	new
		Count,
		animlib[32],
		checkanimlib[32],
		animname[32],
		string[512];
	format(string, 512, ""#NL"\
	new"#NLT"\
		animcmds [][] ="#NL"\ 
	{"#NL"");
	fwrite(jj, string);
	string[0] = '\0';
	for(new i = 1; i < MAXANIM; i++)
	{
		GetAnimationName(i, animlib, 32, animname, 32);
		
		if(strcmp(animlib, checkanimlib) != 0)
		{
			Count++;
			format(string, 512, "%s /%s", string, tolowerstring(checkanimlib));
			if(Count == 10)
			{
				format(string, 512, ""#TAB"{ \"%s\\n\" },"#NL"", string);
				fwrite(jj, string);
				string[0] = '\0';
				Count = 0;
			}
		}
		format(checkanimlib, 32, "%s", animlib);
	}
	if(Count != 0)
	{
		format(string, 512, ""#TAB"{ \"%s /%s\" }"#NL"", string, tolowerstring(animlib));
		fwrite(jj, string);
	}
	fwrite(jj, ""#TAB"{ \" /dance /handsup /pee /phone\" }"#NL"");
	fwrite(jj, "};"#NL"");
	
	format(string, 512, ""#NL"\
	COMMAND:animlist(playerid, params[]) {"#NLT"\
		new"#NLT""#TAB"\
			string[1200];"#NLT"\
		for(new i; i < sizeof animcmds; i++)"#NLT"\
		{"#NLT""#TAB"\
			format(string, sizeof string, \"%%s%%s\", string, animcmds[i]);"#NLT"\
		}"#NLT"\
		ShowPlayerDialog(playerid, 1000, DIALOG_STYLE_MSGBOX, \" Usage: /anim-name animid example: /animdance 1\", string, \"--\", \"--\");"#NLT"\
		return 1;"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock 
	ApplyAnimation_Function(File:jj)
{
	new
		string[512];
	format(string, 512, ""#NL"\
	stock"#NLT"\
		ApplyAnimation2(params[], playerid, animlib[], animname[])"#NL"\
	{"#NLT"\
		new"#NLT""#TAB"\
			time;"#NLT"\
		sscanf(params, \"{d}D(8000)\", time);"#NL""#TAB"\
		ApplyAnimation(playerid, animlib, animname, 4.1, 0, 0, 0, 1, time);"#NLT"\
		PlayerUsingAnim[playerid] = true;"#NLT"\
		TextDrawShowForPlayer(playerid, txtAnim);"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	OnPlayerKeys_Function(File:jj)
{
	new
		string[512];
	format(string, 512, ""#NL"\
	public"#NLT"\
		OnPlayerKeyStateChange(playerid, newkeys, oldkeys)"#NL"\
	{"#NLT"\
		if(pressed(KEY_HANDBRAKE) && PlayerUsingAnim[playerid] == true)"#NLT"\
		{"#NLT""#TAB"\
			StopAnimation(playerid);"#NLT""#TAB"\
			ClearAnimations(playerid);"#NLT""#TAB"\
			SetPlayerSpecialAction(playerid, SPECIAL_ACTION_NONE);"#NLT"\
		}"#NLT"\
		return true;"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	StopAnimation_Function(File:jj)
{
	new
		string[512];
	format(string, 512, ""#NL"\
	stock"#NLT"\
		StopAnimation(playerid)"#NL"\
	{"#NLT"\
		PlayerUsingAnim[playerid] = false;"#NLT"\
		TextDrawHideForPlayer(playerid, txtAnim);"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	OnFilterScriptInit_Function(File:jj)
{
	new
		string[512];
	format(string, 512, ""#NL"\
	public"#NLT"\
		OnFilterScriptInit()"#NL"\
	{"#NLT"\
		print(\" All Animations generated script by Phento\");"#NLT"\
		txtAnim = TextDrawCreate(610.0, 400.0, \"~b~~k~~PED_LOCK_TARGET~ ~w~to stop the animation\");"#NLT"\
		TextDrawUseBox(txtAnim, 0);"#NLT"\
		TextDrawFont(txtAnim, 2);"#NLT"\
		TextDrawSetShadow(txtAnim, 0);"#NLT"");
	fwrite(jj, string);
	format(string, 512, "\
		TextDrawSetOutline(txtAnim,1);"#NLT"\
		TextDrawBackgroundColor(txtAnim,0x000000FF);"#NLT"\
		TextDrawColor(txtAnim,0xFFFFFFFF);"#NLT"\
		TextDrawAlignment(txtAnim,3);"#NLT"\
		return true;"#NLT"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	OnFilterScriptExit_Function(File:jj)
{
	new	
		string[512];
	format(string, 512, ""#NL"\
	public"#NLT"\
		OnFilterScriptExit()"#NL"\
	{"#NLT"\
		TextDrawDestroy(txtAnim);"#NLT"\
		return true;"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	OnPlayerDeath_Function(File:jj)
{
	new
		string[512];
	format(string, 512,""#NL"\
	public"#NLT"\
		OnPlayerDeath(playerid, killerid, reason)"#NL"\
	{"#NLT"\
		if(PlayerUsingAnim[playerid])"#NLT"\
		{"#NLT""#TAB"\
			PlayerUsingAnim[playerid] = false;"#NLT""#TAB"\
			TextDrawHideForPlayer(playerid, txtAnim);"#NLT"\
		}"#NLT"");
	fwrite(jj, string);
	
	format(string, 512, "\
		if(PlayerUsingAnim[killerid])"#NLT"\
		{"#NLT""#TAB"\
			PlayerUsingAnim[killerid] = false;"#NLT""#TAB"\
			TextDrawHideForPlayer(killerid, txtAnim);"#NLT"\
		}"#NLT"\
		return true;"#NL"\
	}"#NL"");
	fwrite(jj, string);
}

stock
	Specialaction_Function(File:jj)
{
	new
		string[512];
	
	format(string, 512, ""#NL"\
	stock"#NLT"\
		SetPlayerSpecialAction2(playerid, actionid)"#NL"\
	{"#NLT"\
		SetPlayerSpecialAction(playerid, actionid);"#NLT"\
		PlayerUsingAnim[playerid] = true;"#NLT"\
		TextDrawShowForPlayer(playerid, txtAnim);"#NL"\
	}"#NL"");
	fwrite(jj, string);
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	format(string, 512, ""#NL"\	
	COMMAND:animpee(playerid, params[])"#NL"\
	{"#NLT"\
		SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_PISSING);"#NLT"\
		return 1;"#NL"\
	}"#NL"");
	fwrite(jj, string);
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	format(string, 512, ""#NL"\
	COMMAND:animhandsup(playerid, params[])"#NL"\
	{"#NLT"\
		SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_HANDSUP);"#NLT"\
		return 1;"#NL"\
	}"#NL"");
	fwrite(jj, string);
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	format(string, 512, ""#NL"\
	COMMAND:animdance(playerid, params[])"#NL"\
	{"#NLT"\
		new"#NLT""#TAB"\
			animid;"#NLT"\
		sscanf(params, \"d\", animid);"#NLT"\
		switch(animid)"#NLT"\
		{"#NLT""#TAB"\
			case 1: SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_DANCE1);"#NLT""#TAB"\
			case 2: SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_DANCE2);"#NLT""#TAB"");
	fwrite(jj, string);

	format(string, 512, "case 3: SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_DANCE3);"#NLT""#TAB"\
			case 4: SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_DANCE4);"#NLT""#TAB"\
			default: SendClientMessage(playerid, COLOR_ANIM, \" Usage: /animdance [1-4]\");"#NLT"\
		}"#NLT"\
		return 1;"#NL"\
	}"#NL"");
	fwrite(jj, string);

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	format(string, 512, ""#NL"\
	COMMAND:animphone(playerid, params[])"#NL"\
	{"#NLT"\
		new"#NLT""#TAB"\
			animid;"#NLT"\
		sscanf(params, \"d\", animid);"#NLT"\
		switch(animid)"#NLT"\
		{"#NLT""#TAB"\
			case 1: SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_USECELLPHONE);"#NLT""#TAB"\
			case 2: { SetPlayerSpecialAction2(playerid, SPECIAL_ACTION_STOPUSECELLPHONE); StopAnimation(playerid); }"#NLT""#TAB"");
	fwrite(jj, string);
	
	format(string, 512, "\
			default: SendClientMessage(playerid, COLOR_ANIM, \" Usage: /animphone [1-2]\");"#NLT"\
		}"#NLT"\
		return 1;"#NL"\
	}");
	fwrite(jj, string);
}

stock 
	tolowerstring(str[])
{
	new
		p = 0,
		rstr[32];
	while(str[p] > ' ') 
	{
		rstr[p] = tolower(str[p]);
		p++;
	}
	rstr[p] = '\0';
	return rstr;
}